package com.org.controller.pc;

import com.org.DTO.LoginDTO;
import com.org.DTO.RegitryDTO;
import com.org.DTO.UpdateUserPasswordDTO;
import com.org.context.BaseContext;
import com.org.entity.User;
import com.org.result.Result;
import com.org.service.UserLoginService;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/pc/user")
@Slf4j
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/user/login")
    public Result login(@RequestBody LoginDTO loginDTO, HttpSession session) {
        log.info("用户登录");
        String token = userLoginService.login(loginDTO, session);
        log.info("用户登录成功");
        return Result.success(token);
    }

    /**
     * 用户注册
     * @param regitryDTO
     * @return
     */
    @PostMapping("/user/registry")
    public Result regitry(@RequestBody RegitryDTO regitryDTO) {
        log.info("用户注册");
        userLoginService.regitry(regitryDTO);
        log.info("用户注册成功");
        return Result.success(null);
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/user/finduser")
    public Result<User> findUser() {
        log.info("查询用户信息");
        User user = userLoginService.findUser();
        return Result.success(user);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PutMapping("/user/update")
    public Result updateUser(@RequestBody User user) {
        log.info("更新用户信息");
        userLoginService.updateUser(user);
        return Result.success(null);
    }

    /**
     * 更新用户密码
     * @param updateUserPasswordDTO
     * @return
     */
    @PutMapping("/user/updatePassword")
    public Result updateUserPassword(@RequestBody UpdateUserPasswordDTO  updateUserPasswordDTO) {
        log.info("更新用户密码");
        userLoginService.updateUserPassword(updateUserPasswordDTO);
        return Result.success(null);
    }

    /**
     * 用户注销
     * @param id
     * @return
     */
    @GetMapping("/user/destroy/{id}")
    public Result destroy(@PathVariable Long id) {
        log.info(BaseContext.getCurrentId() +  "用户注销");
        userLoginService.destroy(id);
        return Result.success("注销成功");
    }

    /**
     * 绑定邮箱
     * @param email 邮箱
     * @param code 验证码
     * @return
     */
    @GetMapping("/user/email")
    public Result bindEmail(String email, String code) {
        log.info("用户" + BaseContext.getCurrentId() + "绑定邮箱");
        userLoginService.bindEmail(email, code);
        return Result.success("绑定邮箱成功");
    }


}
